package com.visikard.asynctask;

import android.os.Handler;
import android.os.Message;

import com.visikard.application.UIMessage;
import com.visikard.business.AccountBUS;
import com.visikard.response.BaseResponse;
import com.visikard.response.MerchantLoginResponse;

public class MerchantLoginTask extends BaseTask {
	public static final int POS_USERNAME = 0;
	public static final int POS_PASSWORD = 1;

	public MerchantLoginTask(Handler handler) {
		super(handler);
	}

	public MerchantLoginTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	/*
	 * (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
	 */
	@Override
	protected Object doBackgroundWork(Object... params) {
		String username = (String) params[POS_USERNAME];
		String password = (String) params[POS_PASSWORD];

		AccountBUS busObj = new AccountBUS();
		MerchantLoginResponse response = busObj.merchantLoginApp(username, password);

		return response;
	}

	@Override
	protected boolean handleResult(Object result) {
		MerchantLoginResponse response = (MerchantLoginResponse) result;
		if (response != null) {
			switch (response.getErrorCode()) {
				case BaseResponse.SUCCESS:
					Message successMsg = new Message();
					successMsg.what = UIMessage.MSG_LOGIN_SUCCESS;
					successMsg.obj = response.getAccount();
					mUIHandler.sendMessage(successMsg);
					return true;
				case MerchantLoginResponse.ERROR_WRONG_USERNAME_PASSWORD:
					mUIHandler.sendEmptyMessage(UIMessage.MSG_WRONG_USERNAME_PASSWORD);
					return true;
			}
		}
		return false;
	}

}
